# 哈希加滑动窗口
class Solution:
    def totalFruit(self, fruits: List[int]) -> int:
        cnt = Counter()

        left = ans = 0
        for right, x in enumerate(fruits):# right与x同时向前
            cnt[x] += 1
            while len(cnt) > 2: # 窗口左移
                cnt[fruits[left]] -= 1
                if cnt[fruits[left]] == 0:
                    cnt.pop(fruits[left])
                left += 1
            ans = max(ans, right - left + 1) # 窗口大小

        return ans